<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传文件 - Venus文件共享</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        .upload-container {
            max-width: 600px;
            margin: 0 auto;
        }

        .header {
            text-align: center;
            margin-bottom: var(--spacing-xl);
        }

        .header h1 {
            font-size: var(--font-size-xl);
            color: var(--text-color);
            margin-bottom: var(--spacing-md);
            transition: color var(--transition-normal);
        }

        .drop-zone {
            padding: var(--spacing-xl);
            border: 2px dashed var(--border-color);
            border-radius: var(--border-radius-lg);
            text-align: center;
            cursor: pointer;
            transition: all var(--transition-normal);
            background-color: var(--background-color);
            margin-bottom: var(--spacing-lg);
        }

        .drop-zone.drag-over {
            border-color: var(--primary-color);
            background-color: rgba(74, 144, 226, 0.1);
        }

        .drop-zone i {
            font-size: 3rem;
            color: var(--primary-color);
            margin-bottom: var(--spacing-md);
        }

        .drop-zone p {
            margin: var(--spacing-sm) 0;
            color: var(--text-color);
            opacity: 0.8;
            transition: color var(--transition-normal);
        }

        .text-secondary {
            opacity: 0.6;
        }

        .file-input {
            display: none;
        }

        .upload-progress {
            display: none;
            margin-top: var(--spacing-lg);
        }

        .progress-bar {
            width: 100%;
            height: 8px;
            background-color: var(--border-color);
            border-radius: var(--border-radius-sm);
            overflow: hidden;
            margin-bottom: var(--spacing-sm);
        }

        .progress-bar-fill {
            height: 100%;
            background-color: var(--primary-color);
            width: 0%;
            transition: width 0.3s ease;
        }

        .upload-status {
            text-align: center;
            color: var(--text-color);
            opacity: 0.8;
        }

        .actions {
            display: flex;
            gap: var(--spacing-md);
            justify-content: center;
            margin-top: var(--spacing-lg);
        }

        .selected-file {
            margin-top: var(--spacing-md);
            padding: var(--spacing-sm) var(--spacing-md);
            background-color: rgba(74, 144, 226, 0.1);
            border-radius: var(--border-radius-sm);
            display: none;
            color: var(--text-color);
            transition: background-color var(--transition-normal), color var(--transition-normal);
        }

        .selected-file i {
            margin-right: var(--spacing-sm);
            color: var(--primary-color);
        }

        /* 暗色模式适配 */
        [data-theme="dark"] .drop-zone {
            background-color: rgba(255, 255, 255, 0.05);
        }

        [data-theme="dark"] .drop-zone.drag-over {
            background-color: rgba(74, 144, 226, 0.2);
        }

        [data-theme="dark"] .selected-file {
            background-color: rgba(74, 144, 226, 0.2);
        }

        [data-theme="dark"] .progress-bar {
            background-color: rgba(255, 255, 255, 0.1);
        }

        [data-theme="dark"] .text-secondary {
            color: rgba(255, 255, 255, 0.6);
        }
    </style>
</head>

<body>
    <div class="container">
        <button class="theme-toggle" onclick="toggleTheme()" title="切换主题">
            <i class="fas fa-moon"></i>
        </button>

        <div class="upload-container">
            <div class="header">
                <h1>上传文件</h1>
            </div>

            <div class="card">
                <form action="{{ url_for('upload_file') }}" method="post" enctype="multipart/form-data" id="upload-form">
                    <div class="drop-zone" id="drop-zone">
                        <i class="fas fa-cloud-upload-alt"></i>
                        <p>拖拽文件到此处或点击选择文件</p>
                        <p class="text-secondary">支持任意类型的文件上传</p>
                        <input type="file" name="file" id="file-input" class="file-input" required>
                    </div>

                    <div class="selected-file" id="selected-file">
                        <i class="fas fa-file"></i>
                        <span id="file-name"></span>
                    </div>

                    <div class="upload-progress" id="upload-progress">
                        <div class="progress-bar">
                            <div class="progress-bar-fill" id="progress-bar-fill"></div>
                        </div>
                        <div class="upload-status" id="upload-status">准备上传...</div>
                    </div>

                    <div class="actions">
                        <button type="submit" class="btn btn-primary" id="upload-btn">
                            <i class="fas fa-upload"></i>
                            开始上传
                        </button>
                        <a href="{{ url_for('index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i>
                            返回首页
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <footer class="footer">
        <p>&copy; {{ now.year }} Venus. All rights reserved.</p>
    </footer>

    <script>
        // 主题切换功能
        function toggleTheme() {
            const html = document.documentElement;
            const themeToggleBtn = document.querySelector('.theme-toggle i');
            const current = html.getAttribute('data-theme');
            const newTheme = current === 'dark' ? 'light' : 'dark';
            
            html.setAttribute('data-theme', newTheme);
            localStorage.setItem('theme', newTheme);
            
            themeToggleBtn.className = newTheme === 'dark' ? 'fas fa-sun' : 'fas fa-moon';
        }

        // 初始化主题
        const savedTheme = localStorage.getItem('theme') || 'light';
        document.documentElement.setAttribute('data-theme', savedTheme);
        const themeToggleBtn = document.querySelector('.theme-toggle i');
        themeToggleBtn.className = savedTheme === 'dark' ? 'fas fa-sun' : 'fas fa-moon';

        // 文件上传相关功能
        const dropZone = document.getElementById('drop-zone');
        const fileInput = document.getElementById('file-input');
        const uploadForm = document.getElementById('upload-form');
        const uploadProgress = document.getElementById('upload-progress');
        const progressBarFill = document.getElementById('progress-bar-fill');
        const uploadStatus = document.getElementById('upload-status');
        const selectedFileDiv = document.getElementById('selected-file');
        const fileNameSpan = document.getElementById('file-name');

        // 拖拽相关事件
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            dropZone.addEventListener(eventName, preventDefaults, false);
            document.body.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        ['dragenter', 'dragover'].forEach(eventName => {
            dropZone.addEventListener(eventName, highlight, false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            dropZone.addEventListener(eventName, unhighlight, false);
        });

        function highlight(e) {
            dropZone.classList.add('drag-over');
        }

        function unhighlight(e) {
            dropZone.classList.remove('drag-over');
        }

        dropZone.addEventListener('drop', handleDrop, false);

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;

            if (files.length > 0) {
                fileInput.files = files;
                updateFileName(files[0]);
            }
        }

        // 点击选择文件
        dropZone.addEventListener('click', () => {
            fileInput.click();
        });

        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                updateFileName(e.target.files[0]);
            }
        });

        function updateFileName(file) {
            fileNameSpan.textContent = file.name;
            selectedFileDiv.style.display = 'block';
        }

        // 处理表单提交
        uploadForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(uploadForm);
            uploadProgress.style.display = 'block';
            progressBarFill.style.width = '0%';
            
            try {
                const response = await fetch(uploadForm.action, {
                    method: 'POST',
                    body: formData
                });

                if (response.ok) {
                    uploadStatus.textContent = '上传成功！';
                    progressBarFill.style.width = '100%';
                    setTimeout(() => {
                        window.location.href = "{{ url_for('index') }}";
                    }, 1000);
                } else {
                    throw new Error('上传失败');
                }
            } catch (error) {
                uploadStatus.textContent = '上传失败，请重试';
                uploadStatus.style.color = 'var(--danger-color)';
            }
        });

        // 添加页面载入动画
        document.addEventListener('DOMContentLoaded', function() {
            document.body.style.opacity = '0';
            setTimeout(() => {
                document.body.style.transition = 'opacity 0.5s ease';
                document.body.style.opacity = '1';
            }, 0);
        });
    </script>
</body>
</html>